package com.ufo.store.management.widget.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.CalendarView;
import android.widget.EditText;

import com.ufo.store.management.R;

import java.sql.Date;

/**
 * Created by taoli on 2018/1/22.
 */

public class CalendarDialog {
    private Activity mActivity;
    private CalendarView mCalendarView = null;
    private int mDayOfMonth;
    private int mMonth;
    private int mYear;


    public CalendarDialog(Activity activity, int year, int month, int day){
        mActivity = activity;
        mDayOfMonth = day;
        mMonth = month;
        mYear = year;
        Date date = new Date(year - 1900, month, day);
        final View view = mActivity.getLayoutInflater().inflate(R.layout.dialog_calendar, null, false);
        mCalendarView = (CalendarView) view.findViewById(R.id.calendarView);
        mCalendarView.setDate(date.getTime());
        mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                mDayOfMonth = dayOfMonth;
                mMonth = month;
                mYear = year;
            }
        });
        AlertDialog alertDialog = new AlertDialog.Builder(mActivity).setTitle("日期选择").setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if( mListener != null ){
                    mListener.onPositiveButtonClick(mDayOfMonth, mMonth, mYear );
                }
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if( mListener != null ){
                    mListener.onNegativeButtonClick();
                }
            }
        }).setCancelable(true).create();
        alertDialog.getWindow().setWindowAnimations(R.style.dialog_anim_center_style);
        alertDialog.show();
    }

    private OnItemClick mListener = null;

    public void setItemClickListener(OnItemClick p) {
        mListener = p;
    }

    public interface OnItemClick {
        void onPositiveButtonClick(int dayOfMonth, int month, int year);
        void onNegativeButtonClick();
    }
}
